package com.foreveross.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HrOptimizedService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HrOptimized;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysHrOptimizedController")
public class SysHrOptimizedController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysHrOptimizedController.class);

	@Resource
	HrOptimizedService hrOptimizedService;

	@Resource
	private LogService logService;
	
	/**
	 * 获取润优选LIST
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrOptimizedList")
	public void getHrOptimizedList(HttpServletRequest request,
			HttpServletResponse response,String userId,String title,String houseId,
			String page,String rows) {
		ResultVo resultVo = new ResultVo();
		try {
			
			int total = (int)hrOptimizedService.getHrOptimizedListCount(null,userId,title, houseId);
			PageVo<HrOptimized> pageVo = new PageVo<HrOptimized>(page,total,rows);
			List<HrOptimized> list = hrOptimizedService.getHrOptimizedList(null,userId,title, houseId,
					pageVo.getpageIndex(),pageVo.getpageSize());
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("润优选 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优选 查看失败");
			log.error("润优选 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看润优选详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHrOptimizedById")
	public void getHrOptimizedById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrOptimized HrOptimized = hrOptimizedService.getHrOptimizedById(id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, HrOptimized.getImage());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(HrOptimized);
			log.info("润优选 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优选  查看失败");
			log.error("润优选 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 添加更新润优选
	 * 
	 * @param request
	 * @param response
	 * @param hrOptimized
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHrOptimized")
	public void addOrUpdateHrOptimized(HttpServletRequest request,
			HttpServletResponse response,String userId, HrOptimized hrOptimized,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			if(Constants.UPDATE_ADD.equals(updateType)){
				hrOptimized.setCreateId(new SysUser(userId));
				hrOptimized.setCreateTime(new Date());
				hrOptimized.setPublisherId(new SysUser(userId));
			}
			hrOptimized.setContentImgList(CommonUtil.getContentImgList(hrOptimized.getContent()));
			hrOptimizedService.saveOrUpdate(hrOptimized, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(
					this.getUserIP(request),
					Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"快递信息" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "润优选["
							+ hrOptimized.toString() + "]");
			log.info("润优选 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优选 " + updateType + "失败");
			log.error("润优选" + updateType + "失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	

	/**
	 * 删除润优选
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHrOptimized")
	public void deleteHrOptimized(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			hrOptimizedService.deleteObjecList("HrOptimized", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),
					Constants.OPERATE_TYPE_BACKGROUND, Constants.LOG_DEL,
					"删除润优选", AuthUtil.getUserid(request), "润优选ID[" + ids + "]");
			log.info("润优选删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优选删除失败");
			log.error("润优选删除失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
